package com.clw.weblogin.service.impl;

import com.clw.weblogin.common.entity.ResultEntity;
import com.clw.weblogin.dao.JieKuanMapper;
import com.clw.weblogin.entity.JieKuan;
import com.clw.weblogin.entity.JieKuanExample;
import com.clw.weblogin.entity.JieKuanLiuShui;
import com.clw.weblogin.service.JieKuanService;
import org.apache.commons.collections.bag.SynchronizedSortedBag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class JieKuanServiceImpl implements JieKuanService {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    @Autowired
    JieKuanMapper jieKuanMapper;

    //获得刚插入的借款数据
    public List<JieKuan> selectByExample(JieKuan entity) {
        JieKuanExample example = new JieKuanExample();
        JieKuanExample.Criteria criteria = example.createCriteria();
        if(entity.getName()!=null){
            criteria.andNameEqualTo(entity.getName());
        }
        if(entity.getBook()!=null){
            criteria.andBookEqualTo(entity.getBook());
        }
        if(entity.getUsernumber()!=null){
            criteria.andUsernumberEqualTo(entity.getUsernumber());
        }
        if(entity.getPhone()!=null){
            criteria.andPhoneEqualTo(entity.getPhone());
        }
        if(entity.getBankcardnumber()!=null){
            criteria.andBankcardnumberEqualTo(entity.getBankcardnumber());
        }
        if(entity.getCunkuanmoney()!=null){
            criteria.andCunkuanmoneyEqualTo(entity.getCunkuanmoney());
        }
        if(entity.getCunkuandate()!=null){
            criteria.andCunkuandateEqualTo(entity.getCunkuandate());
        }
        if(entity.getLilv()!=null){
            criteria.andLilvEqualTo(entity.getLilv());
        } if(entity.getCunkuantype()!=null){
            criteria.andCunkuantypeEqualTo(entity.getCunkuantype());
        }
         if(entity.getDepartmentid()!=null){
            criteria.andDepartmentidEqualTo(entity.getDepartmentid());
        } if(entity.getBeizhu()!=null){
            criteria.andBeizhuEqualTo(entity.getBeizhu());
        } if(entity.getJiesuanzhouqi()!=null){
            criteria.andJiesuanzhouqiEqualTo(entity.getJiesuanzhouqi());
        } if(entity.getCaozuoren()!=null){
            criteria.andCaozuorenEqualTo(entity.getCaozuoren());
        }
        if(entity.getType()!=null){
            criteria.andTypeEqualTo(entity.getType());
        }
        if(entity.getYujilixi()!=null){
            criteria.andYujilixiEqualTo(entity.getYujilixi());
        }

        List<JieKuan> list = jieKuanMapper.selectByExample(example);
        return list;
    }

    //提醒表數據
    public ResultEntity selectByDate(JieKuan entity, int page, int limit) {
        ResultEntity resultEntity = new ResultEntity();
        JieKuanExample example = new JieKuanExample();
        Calendar calendar = Calendar.getInstance();
        JieKuanExample.Criteria criteria = example.createCriteria();
        criteria.andJiesuandateLessThanOrEqualTo(entity.getJiesuandate());
        if (entity.getName() != null) {
            criteria.andNameLike("%" + entity.getName() + "%");
        }
        if (entity.getUsernumber() != null) {
            criteria.andUsernumberLike("%" + entity.getUsernumber() + "%");
        }
        if (entity.getCunkuanmoney() != null) {
            criteria.andCunkuanmoneyEqualTo(entity.getCunkuanmoney());
        }
        if (entity.getCunkuandate() != null) {
            criteria.andCunkuandateEqualTo(entity.getCunkuandate());
        }
        criteria.andTypeEqualTo(entity.getType());
        criteria.andDepartmentidEqualTo(entity.getDepartmentid());
        example.setStart((page - 1) * limit);
        example.setLimit(limit);
        List<JieKuan> list = jieKuanMapper.selectByExample(example);
        if (list.size() >= 1) {
            for(int i = 0; i < list.size(); ++i) {
                String yearStr;
                int month;
                String monthStr;
                int day;
                String dayStr;
                if ((list.get(i)).getCunkuandate() != null) {
                    calendar.setTime((list.get(i)).getCunkuandate());
                    yearStr = calendar.get(Calendar.YEAR) + "";
                    month = calendar.get(Calendar.MONTH) + 1;
                    monthStr = month < 10 ? "0" + month : month + "";
                    day = calendar.get(Calendar.DATE);
                    dayStr = day < 10 ? "0" + day : day + "";
                    (list.get(i)).setStrcunkuandate(yearStr + "/" + monthStr + "/" + dayStr);
                }

                if ((list.get(i)).getEnddate() != null) {
                    calendar.setTime((list.get(i)).getEnddate());
                    yearStr = calendar.get(Calendar.YEAR) + "";
                    month = calendar.get(Calendar.MONTH) + 1;
                    monthStr = month < 10 ? "0" + month : month + "";
                    day = calendar.get(Calendar.DATE);
                    dayStr = day < 10 ? "0" + day : day + "";
                    (list.get(i)).setStrenddate(yearStr + "/" + monthStr + "/" + dayStr);
                }

                if ((list.get(i)).getJiesuandate() != null) {
                    calendar.setTime((list.get(i)).getJiesuandate());
                    yearStr = calendar.get(Calendar.YEAR) + "";
                    month = calendar.get(Calendar.MONTH) + 1;
                    monthStr = month < 10 ? "0" + month : month + "";
                    day = calendar.get(Calendar.DATE);
                    dayStr = day < 10 ? "0" + day : day + "";
                    (list.get(i)).setStrjiesuandate(yearStr + "/" + monthStr + "/" + dayStr);
                }

                try {
                    int qw;
                    if ((list.get(i)).getEnddate() != null) {
                        qw = this.days((list.get(i)).getEnddate(), (list.get(i)).getJiesuandate());
                        (list.get(i)).setDay(qw);
                        (list.get(i)).setShijiday(this.days((list.get(i)).getEnddate(), new Date()));
                    } else {
                        qw = this.days((list.get(i)).getCunkuandate(), (list.get(i)).getJiesuandate());
                        (list.get(i)).setDay(qw);
                        (list.get(i)).setShijiday(this.days((list.get(i)).getCunkuandate(), new Date()));
                    }
                } catch (Exception var15) {
                    System.out.println(var15);
                }

                (list.get(i)).setHavelixi(this.havelixi((list.get(i)).getDay(), (list.get(i)).getShijiday(), Double.valueOf((list.get(i)).getLilv()), (list.get(i)).getCunkuanmoney(), (list.get(i)).getJiesuanzhouqi()));
            }
            Long count = jieKuanMapper.countByExample(example);
            resultEntity.setState(200);
            resultEntity.setData(list);
            resultEntity.setMessage("获取数据");
            resultEntity.setCount(count.intValue());
//            try {
//                List<JieKuan> showlist = new ArrayList<JieKuan>();
//                for (int i = 0; i < list.size(); i++) {
//                    int day;
//                    if (list.get(i).getEnddate() != null) {
//                        day = days(list.get(i).getEnddate(), sdf.parse(sdf.format(new Date())));
//                    } else {
//                        day = days(list.get(i).getCunkuandate(), sdf.parse(sdf.format(new Date())));
//                    }
//                    if (list.get(i).getJiesuanzhouqi() == 30) {
//                        if (day >= 85) {
//                            showlist.add(list.get(i));
//                        }
//                    } else if (list.get(i).getJiesuanzhouqi() == 360) {
//                        if (day >= 355) {
//                            showlist.add(list.get(i));
//                        }
//                    }
//                }
//                List<JieKuan> jieKuanList = new ArrayList<JieKuan>();
//                for (int i = (page - 1) * 7; i < (page - 1) * 7 + 7; i++) {
//                    if (showlist.size() >= (i+1)) {
//                        jieKuanList.add(showlist.get(i));
//                        if (jieKuanList.get(i).getCunkuandate() != null) {
//                            calendar.setTime(jieKuanList.get(i).getCunkuandate());
//                            String yearStr = calendar.get(Calendar.YEAR) + "";//获取年份
//                            int month = calendar.get(Calendar.MONTH) + 1;//获取月份
//                            String monthStr = month < 10 ? "0" + month : month + "";
//                            int day = calendar.get(Calendar.DATE);//获取日
//                            String dayStr = day < 10 ? "0" + day : day + "";
//                            jieKuanList.get(i).setStrcunkuandate(yearStr + "/" + monthStr + "/" + dayStr);
//                        }
//                        if (jieKuanList.get(i).getEnddate() != null) {
//                            calendar.setTime(jieKuanList.get(i).getEnddate());
//                            String yearStr = calendar.get(Calendar.YEAR) + "";//获取年份
//                            int month = calendar.get(Calendar.MONTH) + 1;//获取月份
//                            String monthStr = month < 10 ? "0" + month : month + "";
//                            int day = calendar.get(Calendar.DATE);//获取日
//                            String dayStr = day < 10 ? "0" + day : day + "";
//                            jieKuanList.get(i).setStrenddate(yearStr + "/" + monthStr + "/" + dayStr);
//                        }
//                        if(jieKuanList.get(i).getEnddate()!=null){
//                            jieKuanList.get(i).setShijiday(days(jieKuanList.get(i).getEnddate(),sdf.parse(sdf.format(new Date()))));
//                        }else{
//                            jieKuanList.get(i).setShijiday(days(jieKuanList.get(i).getCunkuandate(),sdf.parse(sdf.format(new Date()))));
//                        }
//
//                        //获得利息
//                        jieKuanList.get(i).setHavelixi(havelixi(0, jieKuanList.get(i).getShijiday(), Double.valueOf(jieKuanList.get(i).getLilv()), jieKuanList.get(i).getCunkuanmoney(), jieKuanList.get(i).getJiesuanzhouqi()));
//                    }else{
//                        break;
//                    }
//                }
//                if (jieKuanList.size() > 0) {
//                    resultEntity.setState(200);
//                    resultEntity.setMessage("获取数据");
//                    resultEntity.setData(jieKuanList);
//                    resultEntity.setCount(showlist.size());
//                }
//            }catch (Exception e){
//                System.out.println(e);
//            }
        } else {
            resultEntity.setMessage("无数据");
        }
        return resultEntity;
    }

    //数据表加载
    public ResultEntity selectByDepartment(JieKuan entity, int page, int limit) {
        JieKuanExample example = new JieKuanExample();
        JieKuanExample.Criteria criteria = example.createCriteria();
        Calendar calendar = Calendar.getInstance();
        criteria.andDepartmentidEqualTo(entity.getDepartmentid());
        if (entity.getName() != null) {
            criteria.andNameLike("%" + entity.getName() + "%");
        }
        if (entity.getUsernumber() != null) {
            criteria.andUsernumberLike("%" + entity.getUsernumber() + "%");
        }
        if (entity.getCunkuanmoney() != null) {
            criteria.andCunkuanmoneyEqualTo(entity.getCunkuanmoney());
        }
        if (entity.getCunkuandate() != null) {
            criteria.andCunkuandateEqualTo(entity.getCunkuandate());
        }
        if(entity.getType()!=null){
            if(entity.getType()!=2){
                criteria.andTypeNotEqualTo(2);
            }else{
                criteria.andTypeEqualTo(entity.getType());
            }

        }else{
            criteria.andTypeNotEqualTo(2);
        }
        //criteria.andJiesuandateGreaterThanOrEqualTo(entity.getJiesuandate());

        example.setStart((page - 1) * limit);
        example.setLimit(limit);
        List<JieKuan> list = jieKuanMapper.selectByExample(example);
        ResultEntity resultEntity = new ResultEntity();
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                //转换日期格式
                if (list.get(i).getCunkuandate() != null) {
                    calendar.setTime(list.get(i).getCunkuandate());
                    String yearStr = calendar.get(Calendar.YEAR) + "";//获取年份
                    int month = calendar.get(Calendar.MONTH) + 1;//获取月份
                    String monthStr = month < 10 ? "0" + month : month + "";
                    int day = calendar.get(Calendar.DATE);//获取日
                    String dayStr = day < 10 ? "0" + day : day + "";
                    list.get(i).setStrcunkuandate(yearStr + "/" + monthStr + "/" + dayStr);
                }
                if (list.get(i).getEnddate() != null) {
                    calendar.setTime(list.get(i).getEnddate());
                    String yearStr = calendar.get(Calendar.YEAR) + "";//获取年份
                    int month = calendar.get(Calendar.MONTH) + 1;//获取月份
                    String monthStr = month < 10 ? "0" + month : month + "";
                    int day = calendar.get(Calendar.DATE);//获取日
                    String dayStr = day < 10 ? "0" + day : day + "";
                    list.get(i).setStrenddate(yearStr + "/" + monthStr + "/" + dayStr);
                }
                if (list.get(i).getJiesuandate() != null) {
                    calendar.setTime(list.get(i).getJiesuandate());
                    String yearStr = calendar.get(Calendar.YEAR) + "";//获取年份
                    int month = calendar.get(Calendar.MONTH) + 1;//获取月份
                    String monthStr = month < 10 ? "0" + month : month + "";
                    int day = calendar.get(Calendar.DATE);//获取日
                    String dayStr = day < 10 ? "0" + day : day + "";
                    list.get(i).setStrjiesuandate(yearStr + "/" + monthStr + "/" + dayStr);
                }
                try { //天数
                    if (list.get(i).getEnddate() != null) {
                        int qw = days(list.get(i).getEnddate(), list.get(i).getJiesuandate());
                        list.get(i).setDay(qw);
                        list.get(i).setShijiday(days(list.get(i).getEnddate(), new Date()));
                    } else {
                        int qw = days(list.get(i).getCunkuandate(), list.get(i).getJiesuandate());
                        list.get(i).setDay(qw);
                        list.get(i).setShijiday(days(list.get(i).getCunkuandate(), new Date()));
                    }
//                    if (list.get(i).getDay() >= 90 && list.get(i).getJiesuanzhouqi() == 30) {
//                        list.get(i).getCunkuandate();
//                    }
                    //预计结算日期   需注意是否超过结束日期
                    if (list.get(i).getEnddate() != null) {
                        list.get(i).setYujijiesuandate(yujidate(list.get(i).getEnddate(), list.get(i).getJiesuanzhouqi()));
                    } else {
                        list.get(i).setYujijiesuandate(yujidate(list.get(i).getCunkuandate(), list.get(i).getJiesuanzhouqi()));
                    }
                    //预计天数
                    if (list.get(i).getYujijiesuandate().getTime() > list.get(i).getJiesuandate().getTime()) {
                        if (list.get(i).getEnddate() != null) {
                            list.get(i).setYujitianshu(days(list.get(i).getEnddate(), list.get(i).getJiesuandate()));
                        } else {
                            list.get(i).setYujitianshu(days(list.get(i).getCunkuandate(), list.get(i).getJiesuandate()));
                        }
                    } else {
                        if (list.get(i).getEnddate() != null) {
                            list.get(i).setYujitianshu(days(list.get(i).getEnddate(), list.get(i).getYujijiesuandate()));
                        } else {
                            list.get(i).setYujitianshu(days(list.get(i).getCunkuandate(), list.get(i).getYujijiesuandate()));
                        }
                    }

                } catch (Exception e) {
                    System.out.println(e);
                }
                //计算利息
                list.get(i).setHavelixi(havelixi(list.get(i).getDay(), list.get(i).getShijiday(), Double.valueOf(list.get(i).getLilv()), list.get(i).getCunkuanmoney(), list.get(i).getJiesuanzhouqi()));
            }
            Long count = jieKuanMapper.countByExample(example);
            resultEntity.setState(200);
            resultEntity.setMessage("获取数据");
            resultEntity.setData(list);
            resultEntity.setCount(count.intValue());
        } else {
            resultEntity.setState(100);
            resultEntity.setMessage("无数据");
        }
        return resultEntity;
    }

    public ResultEntity insert(JieKuan entity) {
        ResultEntity resultEntity = new ResultEntity();
        int i = jieKuanMapper.insert(entity);
        if (i == 1) {
            resultEntity.setState(200);
            resultEntity.setMessage("添加成功");
        } else {
            resultEntity.setState(100);
            resultEntity.setMessage("添加失败");
        }
        return resultEntity;
    }

    public ResultEntity update(JieKuan entity) {
        ResultEntity resultEntity = new ResultEntity();
        JieKuanExample example = new JieKuanExample();
        JieKuanExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(entity.getId());
        jieKuanMapper.updateByExample(entity, example);
        int i = jieKuanMapper.updateByPrimaryKey(entity);
        if (i == 1) {
            //修改成功，流水表中添加记录
            JieKuanLiuShui liuShui = new JieKuanLiuShui();
            liuShui.setJiekuanid(entity.getId());
            resultEntity.setState(200);
            resultEntity.setMessage("成功");
        } else {
            resultEntity.setState(100);
            resultEntity.setMessage("失败");
        }
        return resultEntity;
    }

    //结算时更新 enddate,type,预计利息
    public int updateByid(JieKuan entity) {
        Date enddate = entity.getEnddate();
        double yujilixi = entity.getYujilixi();
        int type = entity.getType();
        int id = entity.getId();
        int i = jieKuanMapper.updateByid(entity);
        return i;
    }

    public ResultEntity deleteByid(JieKuan entity) {
        ResultEntity resultEntity = new ResultEntity();
        int i = jieKuanMapper.deleteByid(entity.getId());
        if (i > 0) {
            resultEntity.setState(200);
            resultEntity.setMessage("删除成功");
        } else {
            resultEntity.setState(100);
            resultEntity.setMessage("删除失败");
        }
        return resultEntity;
    }

    //天数
    public int days(Date cunkuandate, Date jiesuandate) throws Exception { //两个时间差
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.parse(sdf.format(new Date()));
        Calendar calendar_cunkuan = Calendar.getInstance();
        calendar_cunkuan.setTime(cunkuandate);
        int day_cunkuan = calendar_cunkuan.get(Calendar.DATE);
        int month_cunkuan = calendar_cunkuan.get(Calendar.MONTH) + 1;//获取月份
        int year_cunkuan = calendar_cunkuan.get(Calendar.YEAR);//获取年份
        Calendar calendar_jiesuan = Calendar.getInstance();
        calendar_jiesuan.setTime(jiesuandate);
        int year_jiesuan = calendar_jiesuan.get(Calendar.YEAR);
        int month_jiesuan = calendar_jiesuan.get(Calendar.MONTH) + 1;
        int day_jiesuan = calendar_jiesuan.get(Calendar.DATE);
        int year_sub = year_jiesuan - year_cunkuan;
        int month_sub;
        if (month_jiesuan <= month_cunkuan) {
            month_sub = (month_jiesuan + 12 - month_cunkuan) + (year_sub - 1) * 12;
        } else {
            month_sub = month_jiesuan - month_cunkuan + year_sub * 12;
        }
        int maxDay_cunkuan = calendar_cunkuan.getActualMaximum(Calendar.DAY_OF_MONTH); //当前月的最大天数
        if (month_cunkuan == 2 && day_cunkuan >= 28) {
            if(day_jiesuan>=30){
                return 30 + 30 * (month_sub - 1);
            }else{
                return day_jiesuan + 30 * (month_sub - 1);
            }

        }
        if (day_cunkuan == maxDay_cunkuan || day_cunkuan >= 30) {
            month_sub = month_sub - 1;
            if (day_jiesuan < 30) {
                return day_jiesuan + 30 * month_sub;
            } else {
                return 30 + 30 * month_sub;
            }
        } else {
            return (30 - day_cunkuan + day_jiesuan) + 30 * (month_sub - 1);
        }

    }

    //预计结算月份
    public Date yujidate(Date cunkuandate, int jiesuanzhouqi) throws Exception {
        Date yujidate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar_cunkuan = Calendar.getInstance();
        calendar_cunkuan.setTime(cunkuandate);
        int day_cunkuan = calendar_cunkuan.get(Calendar.DATE);
        int month_cunkuan = calendar_cunkuan.get(Calendar.MONTH) + 1;//获取月份
        int year_cunkuan = calendar_cunkuan.get(Calendar.YEAR);//获取年份
        int maxDay_cunkuan = calendar_cunkuan.getActualMaximum(Calendar.DAY_OF_MONTH); //当前月的最大天数
        if (jiesuanzhouqi == 30) {
            if (month_cunkuan == 2 && day_cunkuan >= 28) {   //2月份28 存款
                int mon = month_cunkuan + 3;
                int daya = 28;
                yujidate = sdf.parse(year_cunkuan + "-" + mon + "-" + 30);
            } else if (day_cunkuan >= 30) {
                if (month_cunkuan >= 10) {             //10月份以上存款， 要加一年
                    int mon = month_cunkuan + 3 - 12;
                    int year = year_cunkuan + 1;
                    calendar_cunkuan.setTime(sdf.parse(year + "-" + mon + "-" + 1));
                    int maxday = calendar_cunkuan.getActualMaximum(Calendar.DAY_OF_MONTH);
                    if (maxday < 30) {
                        yujidate = sdf.parse(year + "-" + mon + "-" + maxday);
                    } else {
                        yujidate = sdf.parse(year + "-" + mon + "-" + 30);
                    }
                } else {                        //其他月份存款， 月份+3， 天数30
                    int mon = month_cunkuan + 3;
                    yujidate = sdf.parse(year_cunkuan + "-" + mon + "-" + 30);
                }
            } else { //天数<30
                if (month_cunkuan >= 10) {             //10月份以上存款， 要加一年
                    int mon = month_cunkuan + 3 - 12;
                    int year = year_cunkuan + 1;
                    yujidate = sdf.parse(year + "-" + mon + "-" + day_cunkuan);
                } else {                        //其他月份存款， 月份+3， 天数30
                    int mon = month_cunkuan + 3;
                    yujidate = sdf.parse(year_cunkuan + "-" + mon + "-" + day_cunkuan);
                }
            }
        } else { //按年算
            if (day_cunkuan >= 30) {
                int year = year_cunkuan + 1;
                yujidate = sdf.parse(year + "-" + month_cunkuan + "-" + 30);

            } else {
                int year = year_cunkuan + 1;
                yujidate = sdf.parse(year + "-" + month_cunkuan + "-" + day_cunkuan);
            }

        }
        return yujidate;
    }

    //利息
    //day   结束时间-存款时间
    //shijiday 当前时间-存款时间
    public double havelixi(int day, int shijiday, double lilv, double cunkuanmoney, int zhouqi) {
        if (zhouqi == 30) {
            if (shijiday  >= 30) {
                //拿全部利息
                if ((double) Math.round((cunkuanmoney * lilv / zhouqi * (shijiday-2)) * 100) / 100 < 0) {
                    return 0.00;
                } else {
                    return (double) Math.round((cunkuanmoney * lilv / zhouqi * (shijiday-2)) * 100) / 100;
                }
            } else if (shijiday  < 30) {
                if ((double) Math.round((cunkuanmoney * lilv / zhouqi * (shijiday-2) * 100)) / 100 < 0) {
                    return 0.00;
                } else {
                    return (double) Math.round((cunkuanmoney * lilv / zhouqi * (shijiday-2)) * 100) / 100;
                }
            }
        } else if (zhouqi == 360) {
            if (shijiday >= day) {
                if ((double) Math.round((cunkuanmoney * lilv / zhouqi * (shijiday-2)) * 100) / 100 < 0) {
                    return 0.00;
                } else {
                    return (double) Math.round((cunkuanmoney * lilv / zhouqi * (shijiday-2)) * 100) / 100;
                }
            } else {
                if ((double) Math.round(((cunkuanmoney * lilv / zhouqi * (shijiday-2) ) ) * 100) / 100 < 0) {
                    return 0.00;
                } else {
                    return (double) Math.round(((cunkuanmoney * lilv / zhouqi * (shijiday-2) )) * 100) / 100;
                }
            }
        }
        return 0.00;
    }
}
